/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuoyue.elmt.data.ResultData;
import com.zhuoyue.elmt.entity.WbInspectRecord;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.WbInsuranceRecord;
import com.zhuoyue.elmt.service.WbInsuranceRecordService;

import java.util.ArrayList;
import java.util.List;

/**
 * 保险记录Controller
 * @author JiangYH
 * @version 2017-04-25
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/wbInsuranceRecord")
public class WbInsuranceRecordController extends BaseController {

	@Autowired
	private WbInsuranceRecordService wbInsuranceRecordService;
	
	@ModelAttribute
	public WbInsuranceRecord get(@RequestParam(required=false) String id) {
		WbInsuranceRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = wbInsuranceRecordService.get(id);
		}
		if (entity == null){
			entity = new WbInsuranceRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("elmt:wbInsuranceRecord:view")
	@RequestMapping(value = {"list", ""})
	public String list(WbInsuranceRecord wbInsuranceRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WbInsuranceRecord> page = wbInsuranceRecordService.findPage(new Page<WbInsuranceRecord>(request, response), wbInsuranceRecord); 
		model.addAttribute("page", page);
		return "zhuoyue/elmt/wbInsuranceRecordList";
	}

	@RequiresPermissions("elmt:wbInsuranceRecord:view")
	@RequestMapping(value = "form")
	public String form(WbInsuranceRecord wbInsuranceRecord, Model model) {
		model.addAttribute("wbInsuranceRecord", wbInsuranceRecord);
		return "zhuoyue/elmt/wbInsuranceRecordForm";
	}

	@RequiresPermissions("elmt:wbInsuranceRecord:edit")
	@RequestMapping(value = "save")
	public String save(WbInsuranceRecord wbInsuranceRecord, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, wbInsuranceRecord)){
			return form(wbInsuranceRecord, model);
		}
		wbInsuranceRecordService.save(wbInsuranceRecord);
		addMessage(redirectAttributes, "保存保险记录成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/wbInsuranceRecord/?repage";
	}
	
	@RequiresPermissions("elmt:wbInsuranceRecord:edit")
	@RequestMapping(value = "delete")
	public String delete(WbInsuranceRecord wbInsuranceRecord, RedirectAttributes redirectAttributes) {
		wbInsuranceRecordService.delete(wbInsuranceRecord);
		addMessage(redirectAttributes, "删除保险记录成功");
		return "redirect:"+Global.getAdminPath()+"/elmt/wbInsuranceRecord/?repage";
	}

	@RequestMapping(method = RequestMethod.GET, value = "/mobileget/{str}")
	public String mobileget(@PathVariable String str, HttpServletRequest request, HttpServletResponse response, Model model) {
		ResultData<List<WbInsuranceRecord>> resultData = new ResultData();
		List<WbInsuranceRecord> list = new ArrayList();
		try {
			WbInsuranceRecord entity = null;
			if (StringUtils.isNotBlank(str)) {
				entity = wbInsuranceRecordService.get(str);
			}

			list.add(entity);
			resultData.setList(list);

			resultData.setCode("SUCC");
			resultData.setMessage("获取成功");
		} catch (Exception e) {
			resultData.setCode("FAIL");
			resultData.setMessage("获取异常！");
			e.printStackTrace();
		}
		return super.renderString(response, resultData);
	}

	@RequestMapping(value = "mobilelist")
	public String mobilelist(WbInsuranceRecord wbInsuranceRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		ResultData<List<WbInsuranceRecord>> resultData = new ResultData();
		try {
			Page<WbInsuranceRecord> page = wbInsuranceRecordService.findPage(new Page<WbInsuranceRecord>(request, response), wbInsuranceRecord);
			resultData.setList(page.getList());

			resultData.setCode("SUCC");
			resultData.setMessage("查询成功");
		} catch (Exception e) {
			resultData.setCode("FAIL");
			resultData.setMessage("查询异常！");
			e.printStackTrace();
		}
		return super.renderString(response, resultData);
	}

	@RequestMapping(value = "mobilesave")
	public String mobilesave(WbInsuranceRecord wbInsuranceRecord, HttpServletRequest request, HttpServletResponse response, Model model) {
		ResultData<List<WbInsuranceRecord>> resultData = new ResultData();
		List<WbInsuranceRecord> list = new ArrayList();
		try {
			wbInsuranceRecordService.save(wbInsuranceRecord);
			list.add(wbInsuranceRecord);
			resultData.setList(list);

			resultData.setCode("SUCC");
			resultData.setMessage("保存成功");
		} catch (Exception e) {
			resultData.setCode("FAIL");
			resultData.setMessage("保存异常！");
			e.printStackTrace();
		}
		return super.renderString(response, resultData);
	}

	@RequestMapping(value = "mobiledelete")
	public String mobiledelete(WbInsuranceRecord wbInsuranceRecord, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		ResultData<List<WbInsuranceRecord>> resultData = new ResultData();
		List<WbInsuranceRecord> list = new ArrayList();
		try {
			wbInsuranceRecordService.delete(wbInsuranceRecord);
			list.add(wbInsuranceRecord);
			resultData.setList(list);

			resultData.setCode("SUCC");
			resultData.setMessage("删除成功");
		} catch (Exception e) {
			resultData.setCode("FAIL");
			resultData.setMessage("删除异常！");
			e.printStackTrace();
		}
		return super.renderString(response, resultData);
	}

}